package com.itheima.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.itheima.entity.Orders;
import com.itheima.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.security.SecureRandom;
import java.util.UUID;

@RestController
public class PayController {


    private final String APP_ID = "2021000121673973";
    private final String APP_PRIVATE_KEY = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCoCj4bav5QQNDPn9lz+ypredWf7bo9dzRSLvH69kRRbmNDGoWznxQ6Nix9ejcktYj4JEoHTrCxz1nCkZKk3lC6ia0HiE0gBg/XaOBlYLCFuTuj3gsCNvd7fEmd4HhriI8y9FFtyD/eNnHfZkmi28YrRkggircbSg8YySRPRdilrZi9YShmughyviDX4BrUpHNH+B4FTEIPJpjxknCa8TvxT7UrVEgZW9vKAKiPAJag+++v7tKDl5nKLzQx/y6MADFygn4o/m4AO3vsRame35EraVsqhE5Olz27+VGkpkzt/Rf87prI047Lcfo/xZHz9gRu9EtrhK+1yntQsO5S44TrAgMBAAECggEAGS0nrv3xyl5bqq/daaYY7e9cFVEN1UW13hZQhPQ+uiCa2KPxoj7UTgBt3ALinipgsiTlvoCIsqtJXIBpAzCX0crs1LMUMxcVaoW5ERdSe0aMa/nJlnRb0fGGKhRGln3oTo8SYCtfflCT8Q+nOrJI15T5G38M/hazelgwi3wAgJnqghwL1FpcigcBm83NzV5bcNw5lfe2jyBi98fwFu5fW4CZeAr9X8OXWqu2WSTRTPGMRmyTu5NGNXA+NFYqkMHbq6NKJZZTO8afEQbm+BjF80m0zNHgEq+6KrWMGEavp4dCAWo2KC0HSAdUSnVJzIq4v4cv5qWxzVSr/JG4P+s9WQKBgQDcvbXsX2Qho4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1kXVTlKs5WhFqixnuKIBHiUqX+dZ5kiEriAGQYPzkJMg0Kiz7jfg70yM/qQIQspX/qXRaKbA5eC6Hsy8M3BsJMY6aqLrhbaJdw8ufVy4M9c/5osa0gdMiWDi8Y2zJrt06lJbzSCyFnaquda/Dn1/yxfTqE9N9Sas1athwgTB7CbQ3i5fVsgc4LAczgdhSaH7KBBQCY1yW58YmispYKjDlDZwIDAQAB";
    //这是沙箱接口路径,正式路径为https://openapi.alipay.com/gateway.do
    private final String GATEWAY_URL = "https://openapi.alipaydev.com/gateway.do";
    private final String FORMAT = "JSON";
    //签名方式
    private final String SIGN_TYPE = "RSA2";
    //支付宝异步通知路径,付款完毕后会异步调用本项目的方法,必须为公网地址
    private final String NOTIFY_URL = "http://127.0.0.1/notifyUrl";
    //支付宝同步通知路径,也就是当付款完毕后跳转本项目的页面,可以不是公网地址
    private final String RETURN_URL = "http://localhost:8080//front/page/pay-success.html";
    @Autowired
    private OrderService orderService;

    @RequestMapping("/aliapy")
    public void alipay(HttpServletResponse httpResponse, HttpServletRequest request1) throws IOException {

        SecureRandom r = new SecureRandom();
        //实例化客户端,填入所需参数
        AlipayClient alipayClient = new DefaultAlipayClient(GATEWAY_URL, APP_ID, APP_PRIVATE_KEY, FORMAT, CHARSET, ALIPAY_PUBLIC_KEY, SIGN_TYPE);
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        //在公共参数中设置回跳和通知地址
        request.setReturnUrl(RETURN_URL);
        request.setNotifyUrl(NOTIFY_URL);
        Orders orders = (Orders) request1.getSession().getAttribute("orders");
        //商户订单号，商户网站订单系统中唯一订单号，必填
        //生成随机Id
        String out_trade_no = UUID.randomUUID().toString();
        //付款金额，必填
        String total_amount = orders.getAmount().toString();
        //订单名称，必填
        String subject = "瑞吉点餐";
        //商品描述，可空
        String body = "菩提阁外卖";
        request.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\","
                + "\"total_amount\":\"" + total_amount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"body\":\"" + body + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
        String form = "";
        try {
            form = alipayClient.pageExecute(request).getBody(); // 调用SDK生成表单
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        httpResponse.setContentType("text/html;charset=" + CHARSET);
        httpResponse.getWriter().write(form);// 直接将完整的表单html输出到页面
        httpResponse.getWriter().flush();
        httpResponse.getWriter().close();


    }


}